<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>关于Spring的AOP的学习随记，心情 | HankChan&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="其实今天在理解Spring的过程中，发现对于AOP这个东西竟然还是有些云里雾里的感觉。在理解动态代理的过程中也有一些不太理解的东西。我所看的大致的教学视频是这样的：有一个这样的需求：就是一个日志需求，要求在程序执行期间追踪正在发生的活动。比如，我有一个接口：    public interface ArithmeticCalculator {        int add(int i, int j">
<meta property="og:type" content="article">
<meta property="og:title" content="关于Spring的AOP的学习随记，心情">
<meta property="og:url" content="http://hexo.hankchan.cn/2016/02/19/e5-85-b3-e4-ba-8espring-e7-9a-84aop-e7-9a-84-e5-ad-a6-e4-b9-a0-e9-9a-8f-e8-ae-b0-ef-bc-8c-e5-bf-83-e6-83-85/index.html">
<meta property="og:site_name" content="HankChan&#39;s Blog">
<meta property="og:description" content="其实今天在理解Spring的过程中，发现对于AOP这个东西竟然还是有些云里雾里的感觉。在理解动态代理的过程中也有一些不太理解的东西。我所看的大致的教学视频是这样的：有一个这样的需求：就是一个日志需求，要求在程序执行期间追踪正在发生的活动。比如，我有一个接口：    public interface ArithmeticCalculator {        int add(int i, int j">
<meta property="og:updated_time" content="2017-05-26T02:13:23.615Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="关于Spring的AOP的学习随记，心情">
<meta name="twitter:description" content="其实今天在理解Spring的过程中，发现对于AOP这个东西竟然还是有些云里雾里的感觉。在理解动态代理的过程中也有一些不太理解的东西。我所看的大致的教学视频是这样的：有一个这样的需求：就是一个日志需求，要求在程序执行期间追踪正在发生的活动。比如，我有一个接口：    public interface ArithmeticCalculator {        int add(int i, int j">
  
    <link rel="alternate" href="/atom.xml" title="HankChan&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">HankChan&#39;s Blog</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">生活就像海洋</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://hexo.hankchan.cn"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-e5-85-b3-e4-ba-8espring-e7-9a-84aop-e7-9a-84-e5-ad-a6-e4-b9-a0-e9-9a-8f-e8-ae-b0-ef-bc-8c-e5-bf-83-e6-83-85" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/02/19/e5-85-b3-e4-ba-8espring-e7-9a-84aop-e7-9a-84-e5-ad-a6-e4-b9-a0-e9-9a-8f-e8-ae-b0-ef-bc-8c-e5-bf-83-e6-83-85/" class="article-date">
  <time datetime="2016-02-19T13:50:27.000Z" itemprop="datePublished">2016-02-19</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Spring/">Spring</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      关于Spring的AOP的学习随记，心情
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <div id="wmd-preview-section-18" class="wmd-preview-section preview-content"><br><br>其实今天在理解Spring的过程中，发现对于AOP这个东西竟然还是有些云里雾里的感觉。在理解动态代理的过程中也有一些不太理解的东西。<br><br>我所看的大致的教学视频是这样的：<br>有一个这样的需求：就是一个日志需求，要求在程序执行期间追踪正在发生的活动。<br>比如，我有一个接口：<br><br></div><br><div id="wmd-preview-section-19" class="wmd-preview-section preview-content"><br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> ArithmeticCalculator {<br><br>        <span class="hljs-keyword">int</span> add(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j);<br>        <span class="hljs-keyword">int</span> sub(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j);<br>        <span class="hljs-keyword">int</span> mul(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j);<br>        <span class="hljs-keyword">int</span> div(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j);<br><br>    }<code>&lt;/pre&gt;
    还有一个该接口的实现类：

    &lt;/div&gt;
    &lt;div id=&quot;wmd-preview-section-20&quot; class=&quot;wmd-preview-section preview-content&quot;&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;</code><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArithmeticCalculatorLoggingImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ArithmeticCalculator</span> {</span><br>        <span class="hljs-annotation">@Override</span><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">add</span>(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j) {<br>            System.out.println(<span class="hljs-string">“the method add begins with [“</span> + i + <span class="hljs-string">“,”</span> + j + <span class="hljs-string">“]”</span>);<span class="hljs-comment">//前置的日志方法</span><br>            <span class="hljs-keyword">int</span> result = i + j;<br>            System.out.println(<span class="hljs-string">“the method add edd with “</span> + result);<span class="hljs-comment">//后置的日志方法</span><br>            <span class="hljs-keyword">return</span> result;<br>        }<br><br>        <span class="hljs-annotation">@Override</span><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">sub</span>(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j) {<br>            System.out.println(<span class="hljs-string">“the method sub begins with [“</span> + i + <span class="hljs-string">“,”</span> + j + <span class="hljs-string">“]”</span>);<span class="hljs-comment">//前置的日志方法</span><br>            <span class="hljs-keyword">int</span> result = i - j;<br>            System.out.println(<span class="hljs-string">“the method sub edd with “</span> + result);<span class="hljs-comment">//后置的日志方法</span><br>            <span class="hljs-keyword">return</span> result;<br>        }<br><br>        <span class="hljs-annotation">@Override</span><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">mul</span>(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j) {<br>            System.out.println(<span class="hljs-string">“the method mul begins with [“</span> + i + <span class="hljs-string">“,”</span> + j + <span class="hljs-string">“]”</span>);<span class="hljs-comment">//前置的日志方法</span><br>            <span class="hljs-keyword">int</span> result = i <em> j;<br>            System.out.println(<span class="hljs-string">“the method mul edd with “</span> + result);<span class="hljs-comment">//后置的日志方法</span><br>            <span class="hljs-keyword">return</span> result;<br>        }<br><br>        <span class="hljs-annotation">@Override</span><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">div</span>(<span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j) {<br>            System.out.println(<span class="hljs-string">“the method div begins with [“</span> + i + <span class="hljs-string">“,”</span> + j + <span class="hljs-string">“]”</span>);<span class="hljs-comment">//前置的日志方法</span><br>            <span class="hljs-keyword">int</span> result = i / j;<br>            System.out.println(<span class="hljs-string">“the method div edd with “</span> + result);<span class="hljs-comment">//后置的日志方法</span><br>            <span class="hljs-keyword">return</span> result;<br>        }<br><br>    }<code>&lt;/pre&gt;
    很显然我们发现存在两个问题：
    1.代码混乱：随着业务需求的增加，所有的业务方法急剧膨胀，每个方法在处理核心逻辑问题的同时还必须兼顾到其他的多个关注点。
    2.代码分散：以日志需求为例，只是为了满足这个单一需求，就不得不在多个模块（方法）里多次重复相同的日志代码。如果日志需求发生变化，那就必须修改所有模块了吧。

    解决的办法可以是：使用动态代理！
    代理设计模式的原理是使用一个代理将对象包装起来，然后用该代理对象取代原始对象。任何对原始对象的调用都要经过代理，代理对象决定是否以及何时将方法调用到原始对象上。

    我们看下面这个类：

    &lt;/div&gt;
    &lt;div id=&quot;wmd-preview-section-21&quot; class=&quot;wmd-preview-section preview-content&quot;&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;</code><span class="hljs-keyword">import</span> java.lang.reflect.InvocationHandler;<br>    <span class="hljs-keyword">import</span> java.lang.reflect.Method;<br>    <span class="hljs-keyword">import</span> java.lang.reflect.Proxy;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArithmeticCalculatorLoggProxy</span> {</span><br><br>        <span class="hljs-comment">//要代理的对象</span><br>        <span class="hljs-keyword">private</span> ArithmeticCalculator target;<br><br>        <span class="hljs-keyword">public</span> ArithmeticCalculator <span class="hljs-title">getLoggingProxy</span>() {<br>            ArithmeticCalculator proxy = <span class="hljs-keyword">null</span>;<br><br>            <span class="hljs-comment">//代理对象由哪一个类加载器来加载</span><br>            ClassLoader loader = target.getClass().getClassLoader();<br><br>            <span class="hljs-comment">//代理对象的类型，即其中有哪些方法</span><br>            Class[] interfaces = <span class="hljs-keyword">new</span> Class[]{ArithmeticCalculator.class};<br><br>            <span class="hljs-comment">//当调用代理对象其中的方法时，该执行的代码</span><br>            InvocationHandler h = <span class="hljs-keyword">new</span> InvocationHandler() {<br>                <span class="hljs-javadoc">/**
                 </span></em> proxy: 正在返回的那个代理对象。一般情况下不使用。<br>                 <em> method: 正在被调用的方法
                 </em> args: 调用方法时传入的参数<br>                 */<br>                <span class="hljs-annotation">@Override</span><br>                <span class="hljs-keyword">public</span> Object <span class="hljs-title">invoke</span>(Object proxy, Method method, Object[] args) <span class="hljs-keyword">throws</span> Throwable {<br>                    String methodName = method.getName();<br>                    <span class="hljs-comment">//日志</span><br>                    System.out.println(<span class="hljs-string">“the method of “</span> + methodName + <span class="hljs-string">“is starts with “</span> + Arrays.asList(args));<br>                    <span class="hljs-comment">//执行方法</span><br>                    Object result = method.invoke(target, args);<br>                    <span class="hljs-comment">//日志</span><br>                    System.out.println(<span class="hljs-string">“the method of”</span> + methodName + <span class="hljs-string">“is end with “</span> + result);<br>                    <span class="hljs-keyword">return</span> result;<br>                }<br>            };<br><br>            proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader , interfaces, h);<br><br>            <span class="hljs-keyword">return</span> proxy;<br>        }<br><br>    }<code>&lt;/pre&gt;
    通过这样操作，就可以在开始测试了。
    新建一个测试类：

    &lt;/div&gt;
    &lt;div id=&quot;wmd-preview-section-22&quot; class=&quot;wmd-preview-section preview-content&quot;&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;</code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Main {<br><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) {<br><br>            ArithmeticCalculator arithmeticCalculator = <span class="hljs-keyword">new</span> ArithmeticCalculatorImpl();<span class="hljs-comment">//纯净的。在代理之前的对象</span><br>            ArithmeticCalculator proxy = <span class="hljs-keyword">new</span> ArithmeticCalculatorLoggProxy(arithmeticCalculator).getLoggingProxy();<span class="hljs-comment">//将要代理的对象放入其中，完成代理的操作</span><br><br>            <span class="hljs-comment">//直接使用代理后的对象</span><br>            <span class="hljs-keyword">int</span> result = proxy.add(<span class="hljs-number">2</span>, <span class="hljs-number">5</span>);<br><br>            proxy.div(<span class="hljs-number">12</span>, <span class="hljs-number">3</span>);<br>        }<br><br>    }<br><br>废话就放在后头好了，已经许久没有看自己的博客，当初开博客的初衷是希望自己通过记录分享自己在java学习之路的一些东西，也能在这个过程中提高自己。水平有限，说不上心得，纯粹当是日记吧。<br>现在已经在一家网络科技公司入职，想起来不得不感谢这家公司对我信任，愿意给我一个学习发展、提高自己的机会吧。<br>因为公司所用的框架主要是SpringMVC，所以我也就理所当然地要加强自己SpringMVC框架的学习了。进去之后才发现自己对于Spring和Struts2的理解掌握其实都还很浅，所以自己在工作之余中找出了Spring的学习视频，希望重新深入地对这个框架进行学习掌握。<br>瞎bb了一些，没有思绪写啊。。。。。。。。。。。。。。。<br><br></div>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://hexo.hankchan.cn/2016/02/19/e5-85-b3-e4-ba-8espring-e7-9a-84aop-e7-9a-84-e5-ad-a6-e4-b9-a0-e9-9a-8f-e8-ae-b0-ef-bc-8c-e5-bf-83-e6-83-85/" data-id="cj359k3up0004fwv7fthvsh8k" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/03/26/e5-9f-ba-e4-ba-8e-e9-98-bf-e9-87-8c-e4-ba-91edas-hsf-e3-80-81ali-tomcat-e7-9a-84eclipse-e5-bc-80-e5-8f-91-e7-8e-af-e5-a2-83-e6-90-ad-e5-bb-ba/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          基于阿里云EDAS-HSF、Ali Tomcat的Eclipse开发环境搭建
        
      </div>
    </a>
  
  
    <a href="/2016/01/03/e5-ad-a6-e8-bf-87jdbc-e6-89-8d-e7-9f-a5-e9-81-93-e5-89-8d-e4-b8-80-e7-af-87-e6-96-87-e7-ab-a0-e7-9a-84-e7-bb-8f-e5-8e-86-e5-ae-8c-e5-85-a8-e5-8f-af-e4-bb-a5-e9-81-bf-e5-85-8d-e3-80-82-e3-80-82/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">学过JDBC才知道前一篇文章的经历完全可以避免。。。</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Docker/">Docker</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/">Eclipse</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/Mybatis/">Mybatis</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/Spring/">Spring</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/分布式/">分布式</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/Nginx/">Nginx</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Mybatis/">Mybatis</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Mybatis/Spring/">Spring</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Mybatis/Spring/SpringMVC/">SpringMVC</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Mybatis/SpringMVC/">SpringMVC</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Nginx/">Nginx</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Spring/">Spring</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringMVC/">SpringMVC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/分布式/">分布式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/日志管理/">日志管理</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/未分类/">未分类</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/">五月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">四月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">三月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">二月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">一月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">十二月 2015</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/03/13/docker-e5-be-ae-e6-9c-8d-e5-8a-a1-e5-ae-b9-e5-99-a8-e5-8c-96-e5-bc-80-e5-8f-91-e6-8e-a2-e7-b4-a2/">Docker-微服务容器化开发探索</a>
          </li>
        
          <li>
            <a href="/2017/01/20/drds-e7-bb-93-e5-90-88mybatis-e8-a7-a3-e5-86-b3-e6-96-b9-e6-a1-88-e4-b8-8e-e8-a7-84-e8-8c-83/">DRDS结合Mybatis解决方案与规范</a>
          </li>
        
          <li>
            <a href="/2017/01/03/springmvc-thymeleaf-e5-bc-80-e5-8f-91-e6-8c-87-e5-8d-97-demo/">SpringMVC-Thymeleaf-开发指南-Demo</a>
          </li>
        
          <li>
            <a href="/2016/10/16/nginx-e4-bb-a3-e7-90-86-e6-9c-8d-e5-8a-a1-e5-99-a8-e6-96-b9-e5-bc-8f-e8-a7-a3-e5-86-b3-e5-ae-9e-e9-99-85-e5-bc-80-e5-8f-91-e4-b8-adajax-e8-af-b7-e6-b1-82-e7-9a-84-e8-b7-a8-e5-9f-9f-e9-97-ae-e9-a2-98/">Nginx代理服务器方式解决实际开发中Ajax请求的跨域问题</a>
          </li>
        
          <li>
            <a href="/2016/10/11/e5-b7-a5-e5-8e-82-e6-a8-a1-e5-bc-8f-e5-92-8c-e5-8d-95-e4-be-8b-e6-a8-a1-e5-bc-8f-e7-bb-93-e5-90-88-e5-ba-94-e7-94-a8-e7-9a-84-e5-9c-ba-e6-99-af-e6-80-9d-e8-80-83/">工厂模式和单例模式结合应用的场景思考</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 Hank Chan<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>

  </div>
</body>
</html>